package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.ShopCartListBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class ShopCartDetailAdapter extends RecyclerArrayAdapter<ShopCartListBean.DataBean.ProductsBean> {


    public interface onShopCartDetailInterFace {
        void add(int position);
        void reduce(int position);
        void check(int position);
        void delete(int position);
    }

    private onShopCartDetailInterFace onShopCartDetailInterFace;

    public void setOnShopCartDetailInterFaceListener(onShopCartDetailInterFace listener) {
        onShopCartDetailInterFace = listener;

    }


    public ShopCartDetailAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ShopCartDetailHolder(parent);
    }

    class ShopCartDetailHolder extends BaseViewHolder<ShopCartListBean.DataBean.ProductsBean> {


        @Bind(R.id.img_check)
        ImageView imgCheck;
        @Bind(R.id.img_goods)
        ImageView imgGoods;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.tv_goods_type)
        TextView tvGoodsType;
        @Bind(R.id.img_reduce)
        ImageView imgReduce;
        @Bind(R.id.tv_num)
        TextView tvNum;
        @Bind(R.id.img_add)
        ImageView imgAdd;
        @Bind(R.id.goods_layout)
        RelativeLayout goodsLayout;
        @Bind(R.id.btnDelete)
        Button btnDelete;
        @Bind(R.id.tv_goods_price)
        SuperTextView tvGoodsPrice;
        @Bind(R.id.tv_freight)
        SuperTextView tvFreight;

        public ShopCartDetailHolder(ViewGroup parent) {
            super(parent, R.layout.item_shop_cart_details);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(ShopCartListBean.DataBean.ProductsBean data) {
            super.setData(data);
            Glide.with(BaseActivity.context)
                    .load(data.icon)
                    .apply(MyApplication.options)
                    .into(imgGoods);
            tvGoodsName.setText(data.product_name);
            if (data.option!=null){
                tvGoodsType.setText(data.option.label + " " + data.option.value);
            }
            tvGoodsPrice.setRightString("￥" + data.price);
            tvFreight.setRightString("￥" + data.freight);
            tvNum.setText(data.quantity+"");
            if (data.isSelect) {
                imgCheck.setImageResource(R.drawable.ic_sele);
            } else {
                imgCheck.setImageResource(R.drawable.ic_unsele);
            }

            imgAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onShopCartDetailInterFace!=null){
                        onShopCartDetailInterFace.add(getDataPosition());
                    }
                }
            });
            imgReduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onShopCartDetailInterFace!=null){
                        onShopCartDetailInterFace.reduce(getDataPosition());
                    }
                }
            });
            imgCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onShopCartDetailInterFace!=null){
                        onShopCartDetailInterFace.check(getDataPosition());
                    }
                }
            });
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onShopCartDetailInterFace!=null){
                        onShopCartDetailInterFace.delete(getDataPosition());
                    }
                }
            });

        }
    }
}
